import requests
import sys
import os
sys.path.append(os.path.abspath(os.path.dirname(os.getcwd())))
from common import yaml_util
import pytest
import os
import yaml
from common import config_util
import json
# from request_Util import Requests_Util
import pymysql
#单独存放pytest.fixture()，用于是可以在多个py文件之间共享前置配置。可以有多个，也可以有多个不同层级
#scope=‘作用域’,params='数据驱动',autouse='自动执行',ids=‘自定义参数名’,name=‘重命名’
#作用域function,class,module,package/session
# @pytest.fixture(scope='session')
# def conn_database():
#     print('链接数据库')

# 每次执行前清空通用数据，请求头，token，flagid等
# @pytest.fixture(scope='session',autouse=True)
# def clear_yaml():
#     yaml_util.Yaml_Util().clear_extract()

#登录写入请求头以及token
# @pytest.fixture(scope='session')
# @pytest.fixture(scope='session')
def config():
    # 这里可以根据需要切换环境
    return config_util.Config('test2')

# def login():
#     data = {"username":"admin","password":"tcl123","type":1}
#     url = 'https://xuntian-test2.tclpv.com/api/auth/login'
#     res = requests.post(url,json=data)
#     res.raise_for_status()
#     token = res.json().get('token')
#     headers = {'Authorization': token,'accept': '*/*', 'content-type': 'application/json'}
#     file_path = os.getcwd() + '\extract.yml'
#     if os.path.exists(file_path):
#         with open(file_path, 'r', encoding='utf-8') as f:
#             doc = yaml.load(f, Loader=yaml.FullLoader) or {}
#         # 检查doc是否为空字典
#         if not doc:  # 空字典的真值测试为False
#             # 如果doc为空，则将整个headers字典写入YAML文件
#             with open(file_path, 'w', encoding='utf-8') as f:
#                 yaml.dump(headers, f, allow_unicode=True, default_flow_style=False)
#                 print(f"headers updated to {headers} in {file_path}")
#         else:
#             # 更新或添加 Authorization 字段
#             if 'Authorization' not in doc or doc['Authorization'] != token:
#                 doc['Authorization'] = token
#                 # 将更新后的字典写回 YAML 文件
#                 with open(file_path, 'w', encoding='utf-8') as f:
#                     yaml.dump(doc, f, allow_unicode=True, default_flow_style=False)
#                     # 打印结果（可选）
#                 print(f"Authorization token updated to {token} in {file_path}")
#     else:
#         # 如果文件不存在，则直接写入headers字典
#         with open(file_path, 'w', encoding='utf-8') as f:
#             yaml.dump(headers, f, allow_unicode=True, default_flow_style=False)

    # return yaml_util.Yaml_Util().read_testcase_extract()

# @pytest.fixture(scope='session')
# def header_yaml():
#     print(login)
#     return yaml_util.Yaml_Util().read_extract('extract.yml')

#读取创建的flagid
# @pytest.fixture(scope='function')
# def header_yaml_flagId():
#     return yaml_util.Yaml_Util().read_extract('flagId')
#
# @pytest.fixture(scope='function')
# def connectsql(sql):
#     con = pymysql.connect(host='39.100.84.234',user='test',password='z1p2v0FZdyJPqFBfztIm',database='hy_flag')
#     cur = con.cursor()
#     cur.execute(sql)
#     yield cur.fetchall()
#     con.commit()
#     con.close()
if __name__ == '__main__':
    login()
    # clear_yaml()
    # print(header_yaml)
    # connectsql()
# @pytest.mark.parametrize('caseinfo',yaml_util.Yaml_Util().read_testcase_yaml('get_token.yml'))
# def test_login(self,caseinfo):#得到的caseinfo是字典格式
#     print(caseinfo['request']['method'])
#     res = Requests_Util().send_request(caseinfo['request']['method'],caseinfo['request']['url'],caseinfo['request']['header'],caseinfo['request']['data'])
#     result = json.loads(res)
#     token = result['result']['token']
#     with assume:assert caseinfo['assert'] in result['ret']
#     yaml_util.Yaml_Util().write_extract({'content-type':'application/json','accept': '*/*','Authorization':token})


